//
//  XMBindPhoneView.m
//  XMJJ_zml
//
//  Created by apple on 2025/5/9.
//

#import "XMBindPhoneView.h"

@interface XMBindPhoneView ()

@property (nonatomic, strong) UITextField *phoneTextField;
@property (nonatomic, strong) UITextField *codeTextField;
@property (nonatomic, strong) UIButton *getCodeBtn;

@end

@implementation XMBindPhoneView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self creatUI];
    }
    return self;
}

- (void)creatUI {
    
    UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom frame:CGRectMake(0, kNavagationBarH - 44, 44, 44) image:@"back_icon_black" selectImg:@"back_icon_black"];
    [backBtn addTarget:self action:@selector(backAction:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:backBtn];
    
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(KScaleWidth(28.0), kNavagationBarH + KScaleWidth(5.0), self.width - KScaleWidth(28.0)*2, KScaleWidth(46.0)) font:KBoldFontSize(32.0) textColor:kColorHEX(0x221232)];
    titleLabel.text = @"绑定手机号";
    [self addSubview:titleLabel];
    
    UILabel *explainLabel = [[UILabel alloc] initWithFrame:CGRectMake(KScaleWidth(28.0), titleLabel.maxY + KScaleWidth(5.0), self.width - KScaleWidth(28.0)*2, KScaleWidth(33.0)) font:KFontSize(16.0) textColor:kColorHEX(0x988DA2)];
    explainLabel.text = @"为了您的账户安全，请先绑定手机号";
    [self addSubview:explainLabel];
    
    UIImageView *phoneIcon = [[UIImageView alloc] initWithFrame:CGRectMake(KScaleWidth(28.0), explainLabel.maxY + KScaleWidth(95.0), KScaleWidth(17.0), KScaleWidth(17.0))];
    phoneIcon.contentMode = UIViewContentModeScaleAspectFit;
    phoneIcon.image = ImageNamed(@"login_phone_icon");
    [self addSubview:phoneIcon];
    
    UILabel *phoneTitleLabel = [[UILabel alloc] initWithFrame:CGRectMake(phoneIcon.maxX + KScaleWidth(10.0), phoneIcon.y, self.width - phoneIcon.maxX - KScaleWidth(10.0), phoneIcon.height) font:KFontSize(16.0) textColor:kColorHEX(0x988DA2)];
    phoneTitleLabel.text = @"手机";
    [self addSubview:phoneTitleLabel];
    
    //手机号输入
    UIView *phoneNumBgView = [[UIView alloc] initWithFrame:CGRectMake(KScaleWidth(28.0), phoneIcon.maxY + KScaleWidth(5.0), self.width - KScaleWidth(28.0)*2, KScaleWidth(44.0))];
    phoneNumBgView.backgroundColor = kColorHEX(0xF5F3F7);
    [phoneNumBgView setCornerRadius:phoneNumBgView.height/2];
    [self addSubview:phoneNumBgView];
    
    UIButton *areaSelBtn = [UIButton buttonWithType:UIButtonTypeCustom font:KFontSize(16.0) frame:CGRectMake(0, 0, KScaleWidth(75.0), phoneNumBgView.height) title:@"+86" titleColor:kColorHEX(0x221232) image:@""];
    [areaSelBtn addTarget:self action:@selector(areaSelAction:) forControlEvents:UIControlEventTouchUpInside];
    [phoneNumBgView addSubview:areaSelBtn];
    UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(areaSelBtn.maxX, KScaleWidth(8.0), 1, phoneNumBgView.height - KScaleWidth(8.0)*2)];
    lineView.backgroundColor = kColorHEX(0xCBC3D2);
    [phoneNumBgView addSubview:lineView];
    [phoneNumBgView addSubview:self.phoneTextField];
    
    [self addSubview:self.getCodeBtn];
    _getCodeBtn.y = phoneNumBgView.maxY + KScaleWidth(114.0);
}

- (void)areaSelAction:(UIButton *)button {
    
}

- (void)getCodeAction:(UIButton *)button {
    self.getCodeBlock ? self.getCodeBlock(self.phoneTextField.text) : nil;;
}

- (void)inputChanged:(UITextField *)textField {
    _getCodeBtn.enabled = [textField.text validateMobile];
}

#pragma mark - Init
- (UITextField *)phoneTextField {
    if (!_phoneTextField) {
        _phoneTextField = [[UITextField alloc] initWithFrame:CGRectMake(KScaleWidth(85.0), 0, self.width - KScaleWidth(28.0)*2 - KScaleWidth(85.0), KScaleWidth(44.0))];
        _phoneTextField.font = KFontSize(16.0);
        _phoneTextField.textColor = kColorHEX(0x221232);
        _phoneTextField.placeholder = @"请输入手机号码";
        [_phoneTextField addTarget:self action:@selector(inputChanged:) forControlEvents:UIControlEventEditingChanged];
    }
    return _phoneTextField;
}

- (UIButton *)getCodeBtn {
    if (!_getCodeBtn) {
        _getCodeBtn = [UIButton buttonWithType:UIButtonTypeCustom font:KFontSize(16.0) frame:CGRectMake(KScaleWidth(28.0), 0, self.width - KScaleWidth(28.0)*2, KScaleWidth(44.0)) title:@"获取短信验证码" titleColor:kColorHEX(0xCBC3D2)];
       _getCodeBtn.backgroundColor = kColorHEX(0xF5F3F7);
       [_getCodeBtn setCornerRadius:_getCodeBtn.height/2];
       [_getCodeBtn setBackgroundImage:[UIImage imageWithColor:kColorHEX(0xF5F3F7)] forState:UIControlStateDisabled];
        [_getCodeBtn setTitleColor:kColorHEX(0xCBC3D2) forState:UIControlStateDisabled];
        [_getCodeBtn setBackgroundImage:[UIImage imageWithColor:kColorHEX(0xFFD800)] forState:UIControlStateNormal];
        [_getCodeBtn setTitleColor:kColorHEX(0x333333) forState:UIControlStateNormal];
       [_getCodeBtn addTarget:self action:@selector(getCodeAction:) forControlEvents:UIControlEventTouchUpInside];
        _getCodeBtn.enabled = NO;
    }
    return _getCodeBtn;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
